/**
 *
 */
package net.zantetu.matomeroid.manager;

import java.io.IOException;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

/**
 * @author aono_masashi
 *
 */
public class DBManager extends SQLiteOpenHelper {

	private static final String DB_NAME = "matomeroid";
	private static final int VERSION = 1;
	private Context context;

	/**
	 * @param context
	 */
	public DBManager(Context context) {
		super(context, DB_NAME, null, VERSION);
		this.context = context;
	}

	/* (非 Javadoc)
	 * @see android.database.sqlite.SQLiteOpenHelper#onCreate(android.database.sqlite.SQLiteDatabase)
	 */
	@Override
	public void onCreate(SQLiteDatabase db) {

		try {
			db.beginTransaction();
			String tmp = FileManager.readFile(context, "create_table.sql");
			if(tmp != null){
				// テーブル作成
				String[] createTableSqls = tmp.split(";");
				if(createTableSqls != null && createTableSqls.length > 0){
					for(String createTable : createTableSqls){
						db.execSQL(createTable);
					}
				}

				// 初期データ作成
				tmp = FileManager.readFile(context, "create_data.sql");
				if(tmp != null){
					String[] insertDataSqls = tmp.split(";");
					if(insertDataSqls != null){
						for(String insertData : insertDataSqls){
							db.execSQL(insertData);
						}
					}
				}
			}

			db.endTransaction();
		} catch (IOException e) {}
	}

	/* (非 Javadoc)
	 * @see android.database.sqlite.SQLiteOpenHelper#onUpgrade(android.database.sqlite.SQLiteDatabase, int, int)
	 */
	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		// TODO とりあえず未実装で
	}

}
